package com.neu.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.neu.R;

public class InternetActivity extends AppCompatActivity {
    private Button btnSave;
    private EditText txtIP;
    private EditText txtPort;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_internet);

        initView();
    }



    private void initView() {
        btnSave=findViewById(R.id.btnSave);
        txtIP=findViewById(R.id.txtIP);
        txtPort=findViewById(R.id.txtPort);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获得ip和端口号
                String strIP=txtIP.getText().toString();
                String strPort=txtPort.getText().toString();
                //判断端口号和IP必须有值
                if("".equals(strIP) || "".equals(strPort)){
                    Toast.makeText(getApplication(),"IP或端口号不能为空",Toast.LENGTH_LONG).show();
                }else{
                    //如果不是空
                    //保存数据
                    SharedPreferences  sp=getSharedPreferences("config", Context.MODE_PRIVATE);
                    //编辑
                    SharedPreferences.Editor editor=sp.edit();
                    String ip="http://"+strIP+":"+strPort;
                    editor.putString("ip",ip);
                    editor.putBoolean("isFirst",true);
                    //

                    editor.commit();
                    //关闭activity
                    finish();
                }


            }
        });
    }
}